package com.text;
import com.dao.user.AddressMapper;
import com.dao.user.RoleMapper;
import com.dao.user.UserMapper;
import com.entity.Address;
import com.entity.Role;
import com.entity.User;
import com.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class RoleTest {
    private static Logger logger = Logger.getLogger(RoleTest.class);
    @Test
    public void testAddRole(){
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            Role role = new Role();
            role.setRoleCode("123");
            role.setRoleName("普通员工");
            role.setCreatedBy(1);
            role.setCreationDate(new Date());
            count = sqlSession.getMapper(RoleMapper.class).roleAdd(role);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug(count);
    }
    @Test
    public void testUpdateRole(){
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            Role role = new Role();
            role.setId(4);
            role.setRoleCode("aaa");
            role.setRoleName("经理");
            role.setCreatedBy(1);
            role.setCreationDate(new Date());
            count = sqlSession.getMapper(RoleMapper.class).roleUpdate(role);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug(count);
    }
    @Test
    public void testSmbsm(){
        SqlSession sqlSession = null;
        Integer userRole = 6;
        int result;

        try {
            sqlSession = MyBatisUtil.createSqlSession();

            List<User> userList = sqlSession.getMapper(UserMapper.class).getUserList();

            //删除user表内的用户信息
            if (userList != null) {
                result = sqlSession.getMapper(UserMapper.class).deleteUserByUserRole(userRole);
            }
            result = sqlSession.getMapper(RoleMapper.class).removeById(userRole);

            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            result = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        if (result > 0) {
            logger.debug("----------删除成功！！");
        }
    }
    @Test
    public void testSelectRoleLikeName(){
        SqlSession sqlSession = null;
        List<Role> role = new ArrayList<Role>();

        try {
            sqlSession = MyBatisUtil.createSqlSession();
            role = sqlSession.getMapper(RoleMapper.class).selectLikeName("系");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Role role1 : role){
            logger.debug("testSelectRoleLikeName code"+ role1.getRoleCode());
        }
    }
}
